from django.http import JsonResponse
from django.views.generic import View
from ..utils.literUtils import get_model_serializer_config, type_conversion_dict
from ..serializers.literListSerializer import LiterListQuerySerializer, LiterListSerializer
from ..serializers.literInfoSerializer import LiterCreateSerializer

# 文献展示界面配置
class LiterConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            query_fields = []
            query_fields = LiterListQuerySerializer.get_query_fields()
            for field in query_fields:
                field["type"] = type_conversion_dict[field["type"]]
            data["query_fields"] = query_fields
            table_fields = []
            table_fields = get_model_serializer_config(LiterListSerializer)
            table_extra_fields_info = LiterListSerializer.get_extra_fields_info()
            # print(table_extra_fields_info)
            for extra_fields in table_extra_fields_info:
                for field in table_fields:
                    if field["dataIndex"] == extra_fields["dataIndex"]:
                        field["type"] = extra_fields["type"]
                        field["title"] = extra_fields["title"]
                        break
            # print(table_fields)
            table_fields.append({"title":"操作","dataIndex":"operate","type":"operate"})
            hidden_fields = ['uuid']
            sortable_fields = ["title", "zh_title", "language","date", "liter_type", 'status', 'cit_name', 'cited_count']
            for field in table_fields:
                field["type"] = type_conversion_dict[field["type"]]
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in sortable_fields:
                    field["sortable"] = True
            # print(table_fields)
            # 获取展示字段
            data["table_fields"] = table_fields
            # print(data)
            res["code"] = 200
            res["data"] = data
            res["message"] = "literature配置获取成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)
    
    
# 文献新建界面配置
class LiterAddConfigView(View):
    def get(self, request):
        res = {"code": 400, "message": "", "data": None}
        try:
            data = {}
            add_fields = []
            add_fields = get_model_serializer_config(LiterCreateSerializer)
            hidden_fields = []
            cit_hidden_fields = ["volume", "issue", "first_page", "last_page", "edition_number", "pub_type", 'authors', 'keywords', 'cited_list']
            
            parent_classes_map = {
                'volume': ['Journal'],
                'issue': ['Journal'],
                # 'first_page': ['Journal', 'Book'],
                # 'last_page': ['Journal', 'Book'],
                'edition_number': ['Book'],
            }
            
            for field in add_fields:
                # print(field['dataIndex'] ,field['type'])
                if field['dataIndex'] == 'pub_type':
                    field['type'] = "ForeignKey"
                if field["dataIndex"] in hidden_fields:
                    field["hidden"] = True
                if field["dataIndex"] in cit_hidden_fields:
                    field["citHidden"] = True
                field["type"] = type_conversion_dict[field["type"]]
                if field["type"] == "foreignkey" or field["type"] == 'relation' or field["type"] == 'bool':
                    field["required"] = False
                if field['dataIndex'] in parent_classes_map:
                    field['parentClasses'] = parent_classes_map[field['dataIndex']]
            add_fields.insert(16, {"title":"来源出版物", "dataIndex":"pub_name", "type":"detailForeignkey", 'citHidden': True})
            # extra_fields_info = LiterCreateSerializer.get_extra_fields()
            # for extra_fields  in extra_fields_info:
            #     add_fields.append(extra_fields)
            # print(add_fields)
            data["create_fields"] = add_fields
            res["code"] = 200
            res["data"] = data
            res["message"] = "成功"
        except Exception as e:
            res["code"] = 500
            res["message"] = "服务器错误：" + str(e)
        return JsonResponse(res)